package project.bob.screen;

import java.awt.Graphics;

import project.bob.*;
import project.bob.inventory.Inventory;
import project.bob.level.*;

public class GameScreen extends Screen {
    
    private static final boolean DEBUG_MODE = false;
    private int xLevel = DEBUG_MODE?8:0;
    private int yLevel = DEBUG_MODE?4:0;

    Level level = new Level(this, 32, 24, xLevel, yLevel, 0, 0);
    private Camera camera = new Camera(Bob.GAME_WIDTH, Bob.GAME_HEIGHT);

    public boolean mayRespawn = false;
    private int gunLevel = DEBUG_MODE?2:0;
    
    public GameScreen() {
        Art.level = Art.load("/levels.png");

        level.player.gunLevel = gunLevel;
    }

    public void tick(Input input) {
        if (!input.oldButtons[Input.ESCAPE] && input.buttons[Input.ESCAPE]) {
            setScreen(new PauseScreen(this));
            return;
        }
        if (!input.oldButtons[Input.I] && input.buttons[Input.I]) {
            setScreen(new InventoryScreen(this));
            level.player.inv.hasFocus = true;
            return;
        }
        if (!level.player.removed) level.player.tick(input);
        else if (mayRespawn) {
            if (input.buttons[Input.SHOOT] && !input.oldButtons[Input.SHOOT]) {
                respawnRoom();
                mayRespawn = false;
            }
        }
        level.tick();
    }

    public void transition(int xa, int ya) {
        xLevel += xa;
        yLevel += ya;
        if (yLevel>10) {
            return;
        }
        level.player.x -= xa * 300;
        level.player.y -= ya * 220;
        if (ya != 0) level.player.y -= 10;
        Level newLevel = new Level(this, 32, 24, xLevel, yLevel, (int) (level.player.x), (int) (level.player.y + ya * 5));
        newLevel.player.remove();
        newLevel.player = level.player;
        newLevel.add(newLevel.player);
        setScreen(new LevelTransitionScreen(this, xLevel-xa, yLevel-ya, level, newLevel, xa, ya));
        this.level = newLevel;
        level.player.gunLevel = gunLevel;
        level.player.damage = 0;
    }

    public void render(Graphics g) {
        g.drawImage(Art.bg, -xLevel*160, -yLevel*120, null);
        level.render(g, camera);
        if (mayRespawn) {
            String msg = "PRESS X TO TRY AGAIN";
            drawString(msg, g, 160 - msg.length() * 3, 120 - 3);
        }
    }

    public void respawnRoom() {
        Level newLevel = new Level(this, 32, 24, xLevel, yLevel, level.xSpawn, level.ySpawn);
        this.level = newLevel;
        level.player.gunLevel = gunLevel;
        level.player.damage = 0;
    }

    public void getGun(int level) {
        gunLevel = level;
    }
}
